package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private StatementMapper statementMapper;



    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {

        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void saveItems(List<StatementItem> items) {
        //1.保存结算明细
        if (items!=null &&items.size()>0) {
            //保存最后一行：优惠价格
            StatementItem item = items.remove(items.size() - 1);
            //保存之前先删除之前结算的
            statementItemMapper.deleteByPrimaryKey(item.getStatementId());
            //把列表修改
            BigDecimal total = new BigDecimal("0.00");
            BigDecimal totalAmount = new BigDecimal("0.00");
            if (items.size()>0) {

                //保存结算单明细
                for (StatementItem statementItem : items) {
                    statementItemMapper.insert(statementItem);
                    //总数量=每个明细数量和
                    total = total.add(new BigDecimal(statementItem.getItemQuantity()));
                    //总金额=（每个明细单价*数量）之和
                    totalAmount = totalAmount.add(statementItem.getItemPrice().multiply(new BigDecimal(statementItem.getItemQuantity())));
                }
                //2.更新结算单---总数量，总金额，优惠价
                statementService.updateAmount(item.getStatementId(),total,totalAmount,item.getItemPrice());
            }else {
                statementService.updateAmount(item.getStatementId(),total,totalAmount,new BigDecimal("0.00"));

            }
        }

    }

    @Override
    public void payStatement(Long statementId) {
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        //满足结算条件
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            throw new BusinessException("不能重复支付");
        }
        //设置已支付状态
        statement.setStatus(Statement.STATUS_PAID);
        //修改 收款时间， 收款人
        statement.setPayTime(new Date());
        //收款人：当前登录用户
        statement.setPayeeId(ShiroUtils.getUserId());
        statementMapper.payStatement(statement);
    }

}
